added SSCLI 1.0
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / VBIISAdminWMI / MainModule.vb
blob73d49c9872a7bff726f098e98051921b03618888
1 '***************************** Module Header ******************************\
2 '* Module Name: Program.cs
3 '* Project: VBIISAdminWMI
4 '* Copyright (c) Microsoft Corporation.
5 '*
6 '* This sample demonstrates how to use Windows Management Instrumentation (WMI)
7 '* to configure IIS by using .Net System.Management namespace to access IIS WMI
8 '* Provider. The classes, methods, and properties of the IIS WMI provider can be
9 '* used to configure IIS from scripts or executables.
10 '*
11 '* The IIS WMI provider, like the IIS ADSI provider, provides a standard syntax
12 '* for accessing IIS configuration data through the use of the IIS admin objects.
13 '* Any script or code that attempts to manage IIS using Windows Management
14 '* Instrumentation (WMI) needs to access the IIS WMI provider objects. For example,
15 '* if you want to change a Web site property in the metabase, you must instantiate
16 '* the class called IIsWebServerSetting which is a child of the CIM_Setting class.
17 '* The IIS WMI provider cannot be used without certain methods of the Windows WMI
18 '* classes.
19 '*
20 '* This source is subject to the Microsoft Public License.
21 '* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
22 '* All other rights reserved.
23 '*
24 '* History:
25 '* * 12/24/2009 5:50 PM WenJun Zhang Created
26 '**************************************************************************
29 Imports System
30 Imports System.Collections.Generic
31 Imports System.Linq
32 Imports System.Text
33 Imports System.Management
36 Module VBIISAdminWMI
38 Sub Main()
40 Console.WriteLine("Press any key to create the new web site.")
41 Console.ReadLine()
43 ' Connect to IIS WMI provider.
44 Dim managementScope As New ManagementScope("\\.\root\MicrosoftIISv2")
45 managementScope.Connect()
47 ' Create new server binding.
48 Dim serverBindingClass As New ManagementClass(managementScope, New ManagementPath("ServerBinding"), Nothing)
49 Dim serverBindings As ManagementObject() = New ManagementObject(0) {}
50 serverBindings(0) = serverBindingClass.CreateInstance()
51 serverBindings(0).Properties("Hostname").Value = ""
52 serverBindings(0).Properties("IP").Value = ""
53 serverBindings(0).Properties("Port").Value = "83"
54 serverBindings(0).Put()
56 ' Get W3SVC object.
57 Dim w3svc As New ManagementObject(managementScope, New ManagementPath("IIsWebService='W3SVC'"), Nothing)
59 ' Set parameters of new web site.
60 Dim siteParameters As ManagementBaseObject = w3svc.GetMethodParameters("CreateNewSite")
61 siteParameters("ServerComment") = "IISWMIDemo"
62 siteParameters("ServerBindings") = serverBindings
63 siteParameters("PathOfRootVirtualDir") = "C:\inetpub\IISWMIDemo"
65 ' Create the new web site.
66 Dim ret As ManagementBaseObject = w3svc.InvokeMethod("CreateNewSite", siteParameters, Nothing)
68 ' Retrieve metabase path and site ID.
69 Dim sitePath As String = Convert.ToString(ret.Properties("ReturnValue").Value)
70 Dim siteID As String = sitePath.Replace("IIsWebServer='W3SVC/", "").Replace("'", "")
72 ' Set properties.
73 Dim siteRoot As New ManagementObject(managementScope, New ManagementPath(String.Format("IIsWebVirtualDirSetting.Name='W3SVC/{0}/root'", siteID)), Nothing)
74 siteRoot.Properties("AppFriendlyName").Value = "IISWMIDemo"
75 siteRoot.Properties("AccessRead").Value = True
76 siteRoot.Properties("AuthFlags").Value = 5
77 siteRoot.Properties("AccessScript").Value = True
78 siteRoot.Properties("AuthAnonymous").Value = True
79 siteRoot.Properties("AppPoolId").Value = "DefaultAppPool"
80 siteRoot.Put()
82 Console.WriteLine("Web site created. Starting IISWMIDemo...")
84 ' Wait for a while to allow WMI finish the above operations
85 ' and then start the web site.
86 System.Threading.Thread.Sleep(1000)
87 Dim site As New ManagementObject(managementScope, New ManagementPath(sitePath), Nothing)
88 site.InvokeMethod("Start", Nothing)
90 Console.WriteLine("Started " & vbLf)
92 Console.WriteLine("Create new virtual directory.")
94 ' Create virtual directory
95 Dim vdirPath As String = String.Format("W3SVC/{0}/Root/vdir1", siteID)
97 Dim virtualDirClass As New ManagementClass(managementScope, New ManagementPath("IIsWebVirtualDirSetting"), Nothing)
98 Dim virtualDirObj As ManagementObject = virtualDirClass.CreateInstance()
100 virtualDirObj.Properties("Name").Value = vdirPath
101 virtualDirObj.Properties("Path").Value = "C:\inetpub\IISWMIDemo\vdir1"
102 virtualDirObj.Properties("AuthFlags").Value = 5
103 virtualDirObj.Properties("EnableDefaultDoc").Value = True
104 virtualDirObj.Properties("DirBrowseFlags").Value = &H4000003E
105 virtualDirObj.Properties("AccessFlags").Value = 513
106 virtualDirObj.Put()
108 ' Create web application for this virtual directory
109 Dim virtualDir As New ManagementObject(managementScope, New System.Management.ManagementPath("IIsWebVirtualDir='" & vdirPath & "'"), Nothing)
110 Dim appParameters As ManagementBaseObject = virtualDir.GetMethodParameters("AppCreate2")
111 appParameters("AppMode") = 2
112 virtualDir.InvokeMethod("AppCreate2", appParameters, Nothing)
113 Dim virtualDirSettings As New ManagementObject(managementScope, New System.Management.ManagementPath("IIsWebVirtualDirSetting='" & vdirPath & "'"), Nothing)
114 virtualDirSettings.Properties("AppFriendlyName").Value = "vdir1App"
115 virtualDirSettings.Put()
117 Console.WriteLine("vdir1 created. " & vbLf)
119 ' List all web sites on the server.
120 Console.WriteLine("List all web sites on server: " & vbLf)
122 Dim queryObject As New ObjectQuery("select * from IISWebServerSetting")
123 Dim searchObject As New ManagementObjectSearcher(managementScope, queryObject)
124 For Each result As ManagementObject In searchObject.[Get]()
125 Console.WriteLine((result("ServerComment") & " (") + result("Name") & ")")
126 Next
129 Console.WriteLine(vbLf & "End.")
131 Console.Read()
134 End Sub
136 End Module